

#include "stdafx.h"
#include "pf_previewactioninterface.h"


/**
 * Constructor.
 *
 * Sets the entity container on which the action class inherited 
 * from this interface operates.
 */
PF_PreviewActionInterface::PF_PreviewActionInterface(const char* name,
        PF_Container& container,
        PF_GraphicView& graphicView) :
PF_ActionInterface(name, container, graphicView) {

    PF_DEBUG->print("PF_PreviewActionInterface::PF_PreviewActionInterface: Setting up action with preview: \"%s\"", name);

    // preview is linked to the container for getting access to
    //   document settings / dictionary variables
    preview = new PF_Preview(&container);

    /*
    //extension by ST for handling locked layers
    // 20040101: by andrew: only the selection of entities is 
    // disabled for locked layers
    PF_Document* doc = container.getDocument();
    if (doc!=NULL) {
    	PF_LayerList* layerList = doc->getLayerList();
    	if (layerList!=NULL) {
    		PF_Layer* activeLayer = layerList->getActive();
    		if (activeLayer!=NULL) {
    			if (activeLayer->isLocked()) {
    				finish();
    			}
    		}
    	}
}
    */

    PF_DEBUG->print("PF_PreviewActionInterface::PF_PreviewActionInterface: Setting up action with preview: \"%s\": OK", name);
}



/** Destructor */
PF_PreviewActionInterface::~PF_PreviewActionInterface() {
	graphicView->getOverlayContainer(LX_Define::ActionPreviewEntity)->clear();
	delete preview;
}



void PF_PreviewActionInterface::init(int status) {
    PF_ActionInterface::init(status);
    //deletePreview();
}



void PF_PreviewActionInterface::finish() {
    PF_ActionInterface::finish();
    deletePreview();
}



void PF_PreviewActionInterface::suspend() {
    PF_ActionInterface::suspend();
    deletePreview();
}



void PF_PreviewActionInterface::resume() {
    PF_ActionInterface::resume();
    drawPreview();
}



void PF_PreviewActionInterface::trigger() {
    PF_ActionInterface::trigger();
    deletePreview();
}


/**
 * Deletes the preview from the screen.
 */
void PF_PreviewActionInterface::deletePreview() {
	graphicView->getOverlayContainer(LX_Define::ActionPreviewEntity)->clear();
	preview->clear();
	graphicView->redraw(LX_Define::RedrawOverlay); 
}



/**
 * Draws / deletes the current preview.
 */
void PF_PreviewActionInterface::drawPreview() {
    if (preview && !preview->isEmpty()) {
		//   How does offset work??        painter->setOffset(offset);
		PF_Container *container=graphicView->getOverlayContainer(LX_Define::ActionPreviewEntity);
		container->clear();
		container->setOwner(false); // Little hack for now so we don't delete teh preview twice
		container->addEntity(preview);
		graphicView->redraw(LX_Define::RedrawOverlay);
    }
}

